﻿Imports DevComponents.DotNetBar
Imports BUS
Imports DTO

Public Class frmChoMuonSach : Inherits Office2007Form

    Private Sub btnKiemTra_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKiemTra.Click
        Dim n As Integer
        If (Not Integer.TryParse(txtMaDocGia.Text, n)) Then
            MessageBox.Show("Mã độc giả không đúng", "Thông báo")
            m_danhdau = False
            Return
        End If
        Dim dgia As CDocGiaDTO = New CDocGiaBUS().LayThongTinDocGiaKhiBietMa(n)
        If dgia Is Nothing Then
            MessageBox.Show("Không tồn tại độc giả này", "Thông báo")
            m_danhdau = False
            Return
        End If
        txtTenDocGia.Text = dgia.HoTen
        m_danhdau = True
        m_sosachmuontoida = New CSachBUS().LaySoLuongSachDaMuon(Convert.ToInt32(txtMaDocGia.Text))
    End Sub

    Private Sub Reset()
        cboTinhTrang.SelectedIndex = 1
        cboTinhTrangSach.SelectedIndex = 0
        txtMaSach.Text = ""
        txtMaDocGia.Text = ""
        txtTacGia.Text = ""
        txtTenDocGia.Text = ""
        txtTenSach.Text = ""
        txtTheLoai.Text = ""
        dtpNgayTra.Value = DateTime.Now.AddDays(4)
    End Sub

    Private m_sosachmuontoida As Integer
    Private Sub frmChoMuonSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim lTinhTrangSach As List(Of CTinhTrangSachDTO) = New CTinhTrangSachBUS().LayDanhSachTinhTrangSach()
        For Each tts As CTinhTrangSachDTO In lTinhTrangSach
            cboTinhTrangSach.Items.Add(tts)
        Next
        cboTinhTrangSach.DisplayMember = "Ten"
        cboTinhTrangSach.ValueMember = "Ma"

        Dim lTinhTrangPhieuMuon As List(Of CTinhTrangPhieuMuonDTO) = New CTinhTrangPhieuMuonBUS().LayDanhSachTinhTrangPhieuMuon()
        For Each ttpm As CTinhTrangPhieuMuonDTO In lTinhTrangPhieuMuon
            cboTinhTrang.Items.Add(ttpm)
        Next
        cboTinhTrang.DisplayMember = "Ten"
        cboTinhTrang.ValueMember = "Ma"
        dtpNgayMuon.Value = DateTime.Now
        Reset()
        m_sosachmuontoida = New CThamSoBUS().LaySoLuongMuonToiDa()

    End Sub

    Private m_danhdau As Boolean = True
    Private Sub btnDuyet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDuyet.Click
        Dim ms As Integer
        If (Not Integer.TryParse(txtMaSach.Text, ms)) Then
            MessageBox.Show("Mã sách phải là một số nguyên!", "Thong Bao")
            txtMaSach.Text = ""
            txtMaSach.Focus()
            m_danhdau = False
            Return
        End If

        Dim ttsach As CSachDTO = New CSachBUS().LayThongTinSach(ms)
        If (ttsach Is Nothing) Then
            MessageBox.Show("Cuốn sách này không tồn tại, hoặc đã bị thanh lý!", "Thong Bao")
            txtMaSach.Focus()
            txtMaSach.Text = ""
            m_danhdau = False
            Return
        End If

        txtTenSach.Text = ttsach.Ten
        txtTacGia.Text = ttsach.TacGia
        txtTheLoai.Text = New CTheLoaiSachBUS().LayTenTheLoaiSachTheoMa(Convert.ToInt32(ttsach.MaTheLoai))
        cboTinhTrangSach.SelectedIndex = 0
        m_danhdau = True
    End Sub
    Private stt As Integer = 0
    Private m_soluongdamuon As Integer = 0

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        btnDuyet.PerformClick()
        If Not m_danhdau Then
            Return
        End If

        If (m_soluongdamuon >= m_sosachmuontoida) Then
            MessageBox.Show("Không được mượn quá " + m_sosachmuontoida.ToString() + " quyển sách.", "Thông báo")
            Return
        End If

        For Each dr As DataGridViewRow In dgvChiTietSach.Rows
            If (dr.Cells("clMaSach").Value = txtMaSach.Text) Then
                Return
            End If
        Next
        stt += 1
        Dim tt As CTinhTrangSachDTO = cboTinhTrangSach.SelectedItem
        dgvChiTietSach.Rows.Add(stt, txtMaSach.Text, txtTenSach.Text, dtpNgayTra.Value, tt.Ma)
        m_soluongdamuon += 1
    End Sub

    Private Sub btnDongY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDongY.Click
        btnKiemTra.PerformClick()
        If Not m_danhdau Then
            Return
        End If
        If dgvChiTietSach.Rows.Count = 0 Then
            MessageBox.Show("Chưa nhập sách", "Thông báo")
            Return
        End If

        Dim phieumuon As New CChoMuonSachDTO()
        phieumuon.MaDocGia = txtMaDocGia.Text
        phieumuon.NgayMuon = dtpNgayMuon.Value
        Dim ctinhtrangpm As New CTinhTrangPhieuMuonDTO()
        ctinhtrangpm = cboTinhTrang.SelectedItem
        phieumuon.TinhTrang = ctinhtrangpm.Ma
        Dim kt As Boolean = New CChoMuonSachBUS().ChoMuonSach(phieumuon)
        If (kt) Then
            Dim stlbus As New CChiTiepPhieuMuonBUS()
            Dim stldto As CChiTietPhieuMuonDTO
            For Each dgvr As DataGridViewRow In dgvChiTietSach.Rows
                If dgvr.IsNewRow Then
                    Continue For
                End If
                stldto = New CChiTietPhieuMuonDTO()
                stldto.MaPhieuMuon = phieumuon.Ma
                stldto.MaSach = dgvr.Cells("clMaSach").Value
                stldto.NgayTra = dgvr.Cells("clNgayTra").Value
                stldto.TinhTrang = dgvr.Cells("clTinhTrang").Value
                stlbus.ThemChiTietPhieuMuon(stldto)
                ' dgvr.Cells("clMa").Value = stldto.Ma
            Next
        End If
        MessageBox.Show("Cho mượn thành công", "Thông báo")
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        dgvChiTietSach.Rows.Clear()
        Reset()
    End Sub
End Class